package myJlite.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jvlite.mvc.annotaion.ClzAuth;
import jvlite.mvc.annotaion.Insert;
import jvlite.mvc.annotaion.MethodAuth;
import jvlite.mvc.annotaion.PointCut;
import myJlite.dao.ExamDAO;
import myJlite.dao.IExamDAO;
import myJlite.dao.IMybatisDAO;
import myJlite.entity.ExamVO;
import myJlite.entity.RoleVO;

@PointCut(advice = "examAdvice", value = "")
@ClzAuth(requirePower={"user", "guest"})
public class ExamService implements IExamService {

	@Insert(value = "examDAO")
	public IExamDAO examDAO;
//	private static AuthDataSource authDataSource;
	@Insert(value = "mybatisDAO")
	public IMybatisDAO mybatisDAO;

	public IExamDAO getExamDAO() {
		return examDAO;
	}

	public void setExamDAO(ExamDAO examDAO) {
		this.examDAO = examDAO;
	}

	@Override
	@MethodAuth(requirePower={"admin", "guest"})
	public Integer create(ExamVO examVO) throws Throwable {
//		Object[] args = new Object[] { examVO };
//		Object object = ProxyExecutor.exec("main.dao.ExamDAO", examDAO.getClass()
//				.getMethod("add", ExamVO.class), args);
		Integer usrCnt = examDAO.add(examVO);
		return (Integer) usrCnt;
	}

	@Override
	public ExamVO getByID(Long id) throws SQLException {
		return new ExamDAO().getByID(id);
	}

	@Override
	public ExamVO getByVO(ExamVO examVO) throws SQLException {
		return examDAO.getByVO(examVO);
	}
	
	public List<ExamVO> mybatis(String username) throws SQLException{
//		mybatisDAO = new MybatisDAO();
		List<ExamVO> list = mybatisDAO.getByMybatis(username);
		return list;
	}
	
	public void configDataSource(ExamVO examVO) throws SQLException{
		RoleVO userAuth = this.examDAO.fetchAuth(examVO);
		Integer role_auths = userAuth.getRole_auths().intValue();
		@SuppressWarnings("unused")
		List<String> hasAuthList = null;
		switch (role_auths) {
		case 0:
			hasAuthList = Arrays.asList(new String[]{"guest"});
			break;
		case 1:
			hasAuthList = Arrays.asList(new String[]{"guest", "user"});
			break;
		case 2:
			hasAuthList = Arrays.asList(new String[]{"guest", "operator"});
		case 3:
			hasAuthList = Arrays.asList(new String[]{"guest", "user", "operator"});
		case 5:
			hasAuthList = Arrays.asList(new String[]{"guest", "user", "admin"});
		case 6:
			hasAuthList = Arrays.asList(new String[]{"guest", "operator", "admin"});
		case 7:
			hasAuthList = Arrays.asList(new String[]{"guest", "user", "operator", "admin"});
		default:
			hasAuthList = new ArrayList<String>();
		}
//		DefaultAuthManager.getAuthDataSource().setHasAuthDS(hasAuthList);
	}

}